package com.yang.dao.impl;

import com.yang.dao.CheckOutDao;
import com.yang.entity.LiverRelationEntity;
import com.yang.entity.OrderFormEntity;
import com.yang.entity.RelationEntity;
import org.aspectj.weaver.ast.Or;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Repository
public class CheckOutDaoImpl implements CheckOutDao {

    private static Logger logger = Logger.getLogger(CheckOutDaoImpl.class);
    @Autowired
    SessionFactory sessionFactory;

    @Autowired
    HibernateTemplate hibernateTemplate;

    @Override
    public LiverRelationEntity selectLiveByRoomIdAndStatus(long roomId, String status) {
//        Criteria criteria = sessionFactory.openSession().createCriteria(LiverRelationEntity.class);
//        criteria.add(Restrictions.eq("roomId",roomId));
//        criteria.add(Restrictions.eq("status",status));
//        LiverRelationEntity liverRelationEntity = (LiverRelationEntity) criteria.uniqueResult();
        String hql = "FROM LiverRelationEntity WHERE roomId = ? AND status = ?";
        List<LiverRelationEntity> liverRelationEntity = (List<LiverRelationEntity>) hibernateTemplate.find(hql,new Object[]{roomId,status});
        return liverRelationEntity.isEmpty()?null:liverRelationEntity.get(0);
    }

    @Override
    public boolean updataOrder(OrderFormEntity orderFormEntity) {
        try{
            hibernateTemplate.update(orderFormEntity);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("更新order出错"+orderFormEntity.toString());
            return false;
        }
        return true;
    }

    @Override
    public List<LiverRelationEntity> selectLiverListByOrderId(long orderId) {
        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(LiverRelationEntity.class);
        criteria.add(Restrictions.eq("orderFormId",orderId));
        List<LiverRelationEntity> liverRelationEntities = criteria.list();
        session.close();
        return liverRelationEntities;
    }

    @Override
    public boolean updataLiver(LiverRelationEntity liverRelationEntity) {
        try{
            hibernateTemplate.update(liverRelationEntity);
        }catch (DataAccessException e){
            e.printStackTrace();
            logger.error("更新liver表出错 "+liverRelationEntity);
            return false;
        }
        return true;
    }

    @Override
    public List<RelationEntity> selectRelationByRoomNumAndguestStatus(String roomNum,String status) {
        String hql = "FROM RelationEntity WHERE roomId.roomNum = ? AND guestId.status=?";
        List<RelationEntity> relationEntities = (List<RelationEntity>) hibernateTemplate.find(hql,new Object[]{roomNum,status});
        return relationEntities;
    }

    @Override
    public List<RelationEntity> selectRelationByNameAndguestStatus(String name,String status) {
        String hql = "FROM RelationEntity WHERE guestId.name = ? AND guestId.status=?";
        List<RelationEntity> relationEntities = (List<RelationEntity>) hibernateTemplate.find(hql,new Object[]{name,status});
        return relationEntities;
    }

    @Override
    public OrderFormEntity selectOrderByOrderId(long orderId) {
        String hql = "FROM OrderFormEntity WHERE id = ?";
        List<OrderFormEntity> orderFormEntities = (List<OrderFormEntity>) hibernateTemplate.find(hql,orderId);
        return orderFormEntities.isEmpty()?null:orderFormEntities.get(0);
    }

    @Override
    public List<LiverRelationEntity> selectLiverRelationByOrderId(long orderId) {
        String hql = "FROM LiverRelationEntity WHERE orderFormId = ?";
        List<LiverRelationEntity> liverRelationEntities = (List<LiverRelationEntity>) hibernateTemplate.find(hql,orderId);
        return liverRelationEntities;
    }
}
